import scrapy

from tutorial.items import CookItem


class CookSpider(scrapy.Spider):
    name = "cook"
    load_count = 1
    start_urls = [
        # 家常菜
        'https://machtalk.xiachufang.com/category/40076/',
    ]

    def parse_recipe(self, response):
        cook_item = CookItem()
        # 多个class 使用. 来连接
        cook_item["cook_name"] = response.css("h1.recipe-name.tc::text").get().strip()
        cook_item["material"] = []
        cook_item["procedure"] = []
        for material_quota in response.css("div.recipe-ingredient a"):
            material = material_quota.css("div.ing-name::text").get().strip()
            quota = material_quota.css("div.ing-amount::text").get().strip()
            cook_item["material"].append((material, quota))
        for step_div in response.css("div.step.step"):
            step_txt = step_div.css("p.step-text::text").get().strip()
            cook_item["procedure"].append(step_txt)
        yield cook_item

    def parse(self, response):
        for item in response.css('a.recipe-96-horizon'):
            recipe_href = item.css('a::attr(href)').get()
            recipe_detail_url = response.urljoin(recipe_href)
            yield scrapy.Request(recipe_detail_url, callback=self.parse_recipe)

        load_more_href = response.css('a.addmore::attr(href)').get()
        if self.load_count > 0:
            self.load_count -= 1
            load_more_url = response.urljoin(load_more_href)
            yield scrapy.Request(load_more_url, callback=self.parse)
